package cn.tsoft.taobao.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpEngine {
	private HttpParams httpParams;
	private HttpClient httpClient;

	public String doGet(String url){
		HttpGet httpRequest = new HttpGet(url);
		String strResult = "doGetError";
		try {
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				strResult = EntityUtils.toString(httpResponse.getEntity());
			} else {
				strResult = "Error Response: "
						+ httpResponse.getStatusLine().toString();
			}
		} catch (ClientProtocolException e) {
			strResult = e.getMessage().toString();
			e.printStackTrace();
		} catch (IOException e) {
			strResult = e.getMessage().toString();
			e.printStackTrace();
		} catch (Exception e) {
			strResult = e.getMessage().toString();
			e.printStackTrace();
		}
		Log.v("strResult", strResult);
		return strResult;
	}

	public String doPost(String url,List<NameValuePair> params) {
		HttpPost httpRequest = new HttpPost(url);
		String strResult = "doPostError";
		try {
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				strResult = EntityUtils.toString(httpResponse.getEntity());
			} else {
				strResult = "Error Response: "
						+ httpResponse.getStatusLine().toString();
			}
		} catch (ClientProtocolException e) {
			strResult = e.getMessage().toString();
			e.printStackTrace();
		} catch (IOException e) {
			strResult = e.getMessage().toString();
			e.printStackTrace();
		} catch (Exception e) {
			strResult = e.getMessage().toString();
			e.printStackTrace();
		}

		Log.v("strResult", strResult);

		return strResult;
	}
	
	
   /**
    * 调用TOP API 通用的http参数请求
    * @method 调用API的名称
    * @fields  需要返回的字段
    * @format  返回的格式
    * @isNick 是否需要传入淘宝的用户名
    * @author wanggj
    */
	public   List<NameValuePair>   getTopSessionUrl(String method ,String fields ,String format ,boolean isNick){
		List<NameValuePair> params =new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("v", "2.0"));
		params.add(new BasicNameValuePair("app_key", AuthorInfo.APP_KEY));
		if(!StringUtils.isEmpty(fields))
		params.add(new BasicNameValuePair("fields", fields));
		params.add(new BasicNameValuePair("format",format));
		params.add(new BasicNameValuePair("method", method));
		if(isNick)params.add(new BasicNameValuePair("nick",AuthorInfo.getInstance().nick));
		params.add(new BasicNameValuePair("sign_method", "md5"));
		params.add(new BasicNameValuePair("timestamp",DateTimeUtil.getDateFormat(new Date())));
		params.add(new BasicNameValuePair("session",AuthorInfo.getInstance().topSession));
		return params;
	}

	public HttpClient getHttpClient() {
		this.httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
		HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
		HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
		HttpClientParams.setRedirecting(httpParams, true);
		String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
		HttpProtocolParams.setUserAgent(httpParams, userAgent);
		httpClient = new DefaultHttpClient(httpParams);
		return httpClient;
	}
	
}
